﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using StructureMap;
using TheNorthStar.Web.Application.Mappings;

namespace TheNorthStar.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
        "ApiNoShow", // Route name
        "Drivers/NoShow", // URL with parameters
        new { controller = "Drivers", action = "NoShow", id = UrlParameter.Optional } // Parameter defaults
    );

            routes.MapRoute(
         "ApiCompleteJob", // Route name
         "Drivers/CompleteJob", // URL with parameters
         new { controller = "Drivers", action = "CompleteJob", id = UrlParameter.Optional } // Parameter defaults
     );

            routes.MapRoute(
          "ApiPickedUp", // Route name
          "Drivers/PickedUpBooking", // URL with parameters
          new { controller = "Drivers", action = "PickedUpBooking", id = UrlParameter.Optional } // Parameter defaults
      );

            //drivers/getcustomeraddress
            routes.MapRoute(
           "ApiAcceptJob", // Route name
           "Drivers/AcceptBooking", // URL with parameters
           new { controller = "Drivers", action = "AcceptBooking", id = UrlParameter.Optional } // Parameter defaults
       );

            routes.MapRoute(
             "ApiGetCustomerAddress", // Route name
             "Drivers/GetCustomerAddress", // URL with parameters
             new { controller = "Drivers", action = "GetCustomerAddress", id = UrlParameter.Optional } // Parameter defaults
         );
            //RequestWork


            routes.MapRoute(
            "ApiRequestWork", // Route name
            "Drivers/RequestWork", // URL with parameters
            new { controller = "Drivers", action = "RequestWork", id = UrlParameter.Optional } // Parameter defaults
        );

            routes.MapRoute(
              "ApigetSetDriverStatus", // Route name
              "Drivers/SetDriverStatus", // URL with parameters
              new { controller = "Drivers", action = "SetDriverStatus", id = UrlParameter.Optional } // Parameter defaults
          );

            routes.MapRoute(
     "ApiupdatePosition", // Route name
     "Drivers/updatePosition", // URL with parameters
     new { controller = "Drivers", action = "updatePosition", id = UrlParameter.Optional } // Parameter defaults
 );
            routes.MapRoute(
              "Apigetlogon", // Route name
              "Drivers/{id}", // URL with parameters
              new { controller = "Drivers", action = "Logon", id=UrlParameter.Optional} // Parameter defaults
          );

        
            routes.MapRoute(
           "Apicar", // Route name
           "CarDetails/GetCar", // URL with parameters
           new { controller = "CarDetails", action = "GetCar", id = UrlParameter.Optional } // Parameter defaults
       );
            routes.MapRoute(
               "ApiDefault", // Route name
               "{controller}/{id}", // URL with parameters
               new { controller = "Bookings", action = "Index", id = UrlParameter.Optional } // Parameter defaults
           );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Bookings", action = "Index", id = UrlParameter.Optional} // Parameter defaults
            );

        
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            BookingMaps.Configure();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            InitializeContainer();
        }

        public void InitializeContainer()
        {
            
            //DependencyRegistrar.EnsureDependenciesRegistered();
            var container = (IContainer)IoC.Initialize();
            DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
        }
    }

    public class StructureMapDependencyResolver : IDependencyResolver
    {
        public StructureMapDependencyResolver(IContainer container)
        {
            _container = container;
        }

        public object GetService(Type serviceType)
        {
            if (serviceType.IsAbstract || serviceType.IsInterface)
            {
                return _container.TryGetInstance(serviceType);
            }
            else
            {
                return _container.GetInstance(serviceType);
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _container.GetAllInstances<object>()

                .Where(s => s.GetType() == serviceType);
        }

        private readonly IContainer _container;
    }
}